import asyncio
import json


class AsyncCmdMixin(object):
    async def run_cmd(self, cmd, is_json=False):
        result = await asyncio.create_subprocess_shell(
            cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE
        )

        stdout, stderr = await result.communicate()
        if not is_json:
            self.results = []
            if stderr:
                return
            if stdout:
                self.results = stdout.decode().strip().split('\n')
            return self.results
        print(stdout.decode())
        return json.loads(stdout.decode())

